import request from "../utils/request"
import { get_user_name } from "../utils/decode_jwt"
import { graphql_request, get_now_time_RFC3339 } from "../utils/some_useful"

export function get_user_message() {
  var user_name = get_user_name()
  var data = `query{
    queryUserMessage() {
      id
      receiver(filter:{name:{eq:"${user_name}"}}) {
        name
      }
      send_by(filter:{name:{eq:"peter"}}){
        name
      }
      content
      send_time
      readed
    }
  }`
  return request(graphql_request(data))
}

export function add_friend_query(friend_name) {
  var user_name = get_user_name()
  var send_time = get_now_time_RFC3339()
  var data = `mutation{
    addUserMessage(input:[{
      receiver: {
        name: "${friend_name}"
      },
      send_by: {
        name: "${user_name}"
      },
      content: "Be friends?",
      send_time: "${send_time}",
      readed: false,
    }]) {
      userMessage{
        content
      }
    }
  }`
  return request(graphql_request(data))
}
